/**
 * @file YzhTeacherDetail.vue
 * @description 教师详情页面 - 显示教师的学院系部和教授课程班级
 * @created 2024-07-15
 */

<template>
  <div class="teacher-detail-container">
    <!-- 返回按钮 -->
    <div class="back-btn" @click="goBack">
      <span class="back-icon">←</span> 返回教师列表
    </div>
    
    <!-- 页面标题 -->
    <h1 class="page-title">教师详情</h1>

    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <div class="loading-text">加载教师数据中...</div>
    </div>

      <!-- 教师基本信息卡片 -->
    <div v-else-if="teacher" class="info-card">
        <div class="teacher-header">
          <div class="teacher-avatar">
          <span v-if="teacher.gender === '女'">👩‍🏫</span>
            <span v-else>👨‍🏫</span>
        </div>
        <div class="teacher-name-info">
          <h2>{{ teacher.name }}</h2>
          <div class="teacher-meta">
            <span class="teacher-code">工号: {{ teacher.teacher_code }}</span>
            <span class="teacher-title">{{ teacher.title || '未设置职称' }}</span>
          </div>
            </div>
        <div class="teacher-department-info">
          <div class="department-item">
            <span class="label">所属学院:</span>
            <span class="value">{{ teacher.college_name || '未分配' }}</span>
          </div>
          <div class="department-item">
            <span class="label">所属系部:</span>
            <span class="value">{{ teacher.department_name || '未分配' }}</span>
          </div>
        </div>
      </div>
    </div>
    
    <!-- 教授课程列表 -->
    <div class="section-container">
      <h2 class="section-title">教授课程与班级</h2>
      
      <div v-if="loadingCourses" class="loading-container">
        <div class="loading-spinner"></div>
        <div class="loading-text">加载课程数据中...</div>
              </div>
      
      <div v-else-if="courses.length === 0" class="empty-data">
        暂无课程数据
              </div>
      
      <div v-else class="courses-container">
        <!-- 课程信息卡片 -->
        <div v-for="course_assignment in courses" :key="course_assignment.assignment_id" class="course-card">
          <div class="course-header">
            <h3 class="course-title">{{ course_assignment.course_name }}</h3>
            <span class="course-code">课程编号: {{ course_assignment.course_code }}</span>
              </div>
          
          <div class="course-info">
              <div class="info-item">
              <span class="label">学分:</span>
              <span class="value">{{ course_assignment.credit || '未设置' }}</span>
              </div>
              <div class="info-item">
              <span class="label">学时:</span>
              <span class="value">{{ course_assignment.hours || '未设置' }}</span>
              </div>
              <div class="info-item">
              <span class="label">类型:</span>
              <span class="value">{{ course_assignment.course_type || '未设置' }}</span>
            </div>
          </div>

          <!-- 显示该课程对应的班级信息 -->
          <div class="classes-section">
            <h4 class="classes-title">授课班级</h4>
            
            <div class="classes-list">
              <!-- 由于每个 course_assignment 已经代表一个特定班级的课程，我们直接显示 -->
              <div class="class-item">
                <div class="class-info">
                  <h5 class="class-name">{{ course_assignment.class_name }}</h5>
                  <div class="class-details">
                    <!-- 如果后端能提供学生数量和学期信息，则显示 -->
                    <span class="student-count">{{ course_assignment.student_count || 'N/A' }}名学生</span>
                    <span class="semester">{{ course_assignment.semester || course_assignment.grade_year || '' }}</span>
        </div>
      </div>
                <div class="class-actions">
                  <button class="view-btn" @click="viewClassGrades(teacher.id, course_assignment.course_id, course_assignment.class_id)">
                    查看成绩
                  </button>
        </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
/**
 * 教师详情页面组件
 * @description 显示教师详情、所教授课程以及班级信息
 */
import { ref, onMounted } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { getTeacherDetail, getTeacherCourses } from '@/api/admin/yzhTeacher';
import axios from 'axios';
import request from '@/utils/request';

const router = useRouter();
const route = useRoute();
const teacherId = ref(route.params.teacherId);

// 数据状态
const loading = ref(true);
const loadingCourses = ref(true);
const teacher = ref(null);
const courses = ref([]);

// 调试信息
console.log('当前BaseURL配置:', request.defaults.baseURL);
console.log('API请求将发送到:', `${request.defaults.baseURL}/api/admin/teachers/${teacherId.value}/courses/`);

// 获取教师详情
const fetchTeacherDetail = async () => {
  try {
    loading.value = true;
    const res = await getTeacherDetail(teacherId.value);
    
    if (res.status === 'success' && res.data && res.data.teacher) {
      teacher.value = res.data.teacher;
      console.log('教师详情:', teacher.value);
    } else {
      console.error('获取教师详情失败:', res);
    }
  } catch (error) {
    console.error('获取教师详情失败:', error);
  } finally {
    loading.value = false;
  }
};

// 获取教师课程和班级
const fetchTeacherCourses = async () => {
  try {
    loadingCourses.value = true;
    
    const res = await getTeacherCourses(teacherId.value);
    
    if (res && res.status === 'success' && res.data && Array.isArray(res.data)) {
      courses.value = res.data;
      console.log('获取教师课程成功:', courses.value);
    } else {
      console.error('获取教师课程数据结构不符合预期或API失败:', res);
      courses.value = [];
    }
  } catch (error) {
    console.error('请求教师课程API时发生错误:', error);
    courses.value = [];
  } finally {
    loadingCourses.value = false;
  }
};

// 查看班级学生成绩
const viewClassGrades = (teacherId, courseId, classId) => {
  router.push(`/admin/teacher/${teacherId}/course/${courseId}/class/${classId}/grades`);
};

// 返回教师列表
const goBack = () => {
  router.push('/admin/teacher');
};

// 页面加载时获取数据
onMounted(async () => {
  await fetchTeacherDetail();
  await fetchTeacherCourses();
});
</script>

<style scoped>
.teacher-detail-container {
  padding: 30px;
  max-width: 1200px;
  margin: 0 auto;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  margin-bottom: 20px;
  padding: 8px 16px;
  background-color: #f0f2f5;
  border-radius: 4px;
  transition: all 0.3s;
}

.back-btn:hover {
  background-color: #e6f7ff;
  color: #1890ff;
}

.back-icon {
  margin-right: 8px;
  font-size: 16px;
  font-weight: bold;
}

.page-title {
  font-size: 24px;
  margin-bottom: 30px;
  color: #1a1a1a;
}

.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 0;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 15px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.loading-text {
  color: #666;
  font-size: 14px;
}

.info-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
  margin-bottom: 30px;
}

.teacher-header {
  display: flex;
  align-items: center;
}

.teacher-avatar {
  font-size: 42px;
  margin-right: 20px;
  width: 80px;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f0f7ff;
  border-radius: 50%;
}

.teacher-name-info {
  flex: 1;
}

.teacher-name-info h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: #1a1a1a;
}

.teacher-meta {
  display: flex;
  gap: 15px;
}

.teacher-code {
  color: #1890ff;
  font-size: 14px;
}

.teacher-title {
  color: #666;
  font-size: 14px;
}

.teacher-department-info {
  margin-left: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.department-item {
  display: flex;
  align-items: center;
}

.department-item .label {
  color: #666;
  margin-right: 10px;
  min-width: 70px;
  text-align: right;
}

.department-item .value {
  color: #1a1a1a;
  font-weight: 500;
}

.section-container {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  padding: 20px;
}

.section-title {
  font-size: 18px;
  margin: 0 0 20px 0;
  padding-bottom: 15px;
  border-bottom: 1px solid #f0f0f0;
  color: #1a1a1a;
}

.empty-data {
  text-align: center;
  padding: 30px;
  color: #999;
  font-size: 14px;
  background-color: #fafafa;
  border-radius: 4px;
}

.empty-data.small {
  padding: 15px;
  font-size: 13px;
}

.courses-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
  gap: 20px;
}

.course-card {
  border: 1px solid #eaeaea;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
}

.course-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
}

.course-header {
  padding: 15px;
  background-color: #f0f7ff;
  border-bottom: 1px solid #eaeaea;
}

.course-title {
  margin: 0 0 5px 0;
  font-size: 16px;
  color: #1a1a1a;
}

.course-code {
  font-size: 12px;
  color: #666;
}

.course-info {
  display: flex;
  flex-wrap: wrap;
  padding: 10px 15px;
  background-color: #fafafa;
  border-bottom: 1px solid #f0f0f0;
}

.info-item {
  display: flex;
  align-items: center;
  margin-right: 15px;
  font-size: 13px;
}

.info-item .label {
  color: #666;
  margin-right: 5px;
}

.info-item .value {
  color: #1a1a1a;
}

.classes-section {
  padding: 15px;
}

.classes-title {
  font-size: 14px;
  margin: 0 0 10px 0;
  color: #1a1a1a;
}

.classes-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.class-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px;
  background-color: #f9f9f9;
  border-radius: 4px;
  border-left: 3px solid #1890ff;
}

.class-info {
  flex: 1;
}

.class-name {
  margin: 0 0 5px 0;
  font-size: 14px;
  color: #1a1a1a;
}

.class-details {
  display: flex;
  gap: 10px;
  font-size: 12px;
  color: #666;
}

.class-actions {
  margin-left: 10px;
}

.view-btn {
  background-color: #e6f7ff;
  color: #1890ff;
  border: none;
  padding: 5px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  transition: all 0.3s;
}

.view-btn:hover {
  background-color: #bae7ff;
}
</style> 